package com.shujia.day12;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Code20Collections {
    public static void main(String[] args) {
        /*
          Collections工具类常用的方法：
            public static <T> void sort(List<T> list)  // 对传入的List集合进行排序
            public static <T> int binarySearch(List<?> list,T key) 二分查找
            public static <T> T max(Collection<?> coll) // 取最大值
            public static void reverse(List<?> list)  // 反转
            public static void shuffle(List<?> list)  // 洗牌 对数据进行打乱 每次结果不一样

         */

        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(3);
        integers.add(4);
        integers.add(2);
        integers.add(6);
        integers.add(3);
        Collections.sort(integers);
        System.out.println(integers);


        List<Code20Collections.Student> students = Arrays.asList(
                new Code20Collections.Student("张三", 180),
                new Code20Collections.Student("李四", 160),
                new Code20Collections.Student("王五", 200)
        );

        Collections.sort(students);
        System.out.println(students);



        int binarySearch = Collections.binarySearch(integers, 4);
        System.out.println(binarySearch);

        Integer max = Collections.max(integers);
        System.out.println(max);

        Collections.reverse(integers);
        System.out.println(integers);

        Collections.shuffle(integers);
        System.out.println(integers);


    }

    static class Student implements Comparable<Code20Collections.Student>{

        String name;
        int score;

        public Student() {
        }

        public Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", score=" + score +
                    '}';
        }

        @Override
        public int compareTo(Student o) {
            int ageRes = this.score - o.score;
            return ageRes == 0 ? this.name.compareTo(o.name): ageRes;
        }
    }
}
